﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace FangChanCrm.Controllers
{
    public class GongGaoController : Controller
    {
        private readonly It_GongGaoBiaoService t_GongGaoBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;

        public GongGaoController(It_GongGaoBiaoService gongGaoBiaoService, It_RenYuanBiaoService renYuanBiaoService, IConfiguration configuration)
        {
            t_GongGaoBiaoService = gongGaoBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public IActionResult GongGaoUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult GongGaoAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult GongGaoList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult GongGaoShenPiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string GongGaoAddFangFa()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            string gonggaoneirong = Request.Form["textArea3"];
            string BiaoTi = Request.Form["BiaoTi"];

            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);

            t_GongGaoBiao gonggao = new t_GongGaoBiao();
            gonggao.FangBuRenId = id;
            gonggao.GongGaoNeiRong = gonggaoneirong;
            gonggao.BeiZhu1 = DateTime.Now.ToString("yyyy年MM月dd日");
            gonggao.GongGaoDengJi = ry.t_GangWeiBiao.DengJi.ToString();
            gonggao.BeiZhu2 = "待审批";
            gonggao.BeiZhu3 = BiaoTi;
            string panduan = "";
            panduan = t_GongGaoBiaoService.GongGaoAdd(gonggao);
            return panduan.ToString();
        }

        public string InitializationDataTable()
        {
            t_GongGaoBiao t_GongGaoBiao = new t_GongGaoBiao();
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            string BiaoTi = Request.Form["BiaoTi"];
            string KaiShiShiJian = Request.Form["KaiShiShiJian"];
            string slt_ZhuangTai = Request.Form["slt_ZhuangTai"];
            if (BiaoTi != "")
            {
                t_GongGaoBiao.BeiZhu3 = BiaoTi;
            }
            if (KaiShiShiJian != "")
            {
                t_GongGaoBiao.BeiZhu1 = KaiShiShiJian;
            }
            if (slt_ZhuangTai != "0")
            {
                t_GongGaoBiao.BeiZhu2 = slt_ZhuangTai;
            }
            t_GongGaoBiao.FangBuRenId = id;

            //t_GongGaoBiao.BeiZhu2 = "待审批";
            return t_GongGaoBiaoService.GetListWithFenYe(t_GongGaoBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        public string InitializationDataTable1()
        {
            t_GongGaoBiao t_GongGaoBiao = new t_GongGaoBiao();
            string BiaoTi = Request.Form["BiaoTi"];
            string KaiShiShiJian = Request.Form["KaiShiShiJian"];
            string slt_ZhuangTai = Request.Form["slt_ZhuangTai"];
            if (BiaoTi != "")
            {
                t_GongGaoBiao.BeiZhu3 = BiaoTi;
            }
            if (KaiShiShiJian != "")
            {
                t_GongGaoBiao.BeiZhu1 = KaiShiShiJian;
            }
            if (slt_ZhuangTai != "0")
            {
                t_GongGaoBiao.BeiZhu2 = slt_ZhuangTai;
            }

            //t_GongGaoBiao.BeiZhu2 = "待审批";
            return t_GongGaoBiaoService.GetListWithFenYe(t_GongGaoBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        public string GongGaoHuiBang()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            t_GongGaoBiao t_GongGaoBiao = t_GongGaoBiaoService.GetId(Id);
            string shuju = "{\"neiRong\":\"" + t_GongGaoBiao.GongGaoNeiRong + "\",\"zhuangTai\":\"" + t_GongGaoBiao.BeiZhu2 + "\"}";
            return shuju;
            //return t_GongGaoBiao.GongGaoNeiRong;
        }
        public string GongGaoShenPi()
        {
            string fanhuizhi = "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var GongGao = _context.t_GongGaoBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                if (Request.Form["panduan"] == "1")
                {
                    GongGao.BeiZhu2 = "已批准"; fanhuizhi += "已批准";
                }
                else if (Request.Form["panduan"] == "2")
                {
                    GongGao.BeiZhu2 = "已驳回"; fanhuizhi += "已驳回";
                }
                else
                {
                    fanhuizhi += "审批出错";
                }
                _context.SaveChanges();
            }
            return fanhuizhi;
        }
        public string QiYong()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var GongGao = _context.t_GongGaoBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                GongGao.BeiZhu2 = "启用";
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string JinYong()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var GongGao = _context.t_GongGaoBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                GongGao.BeiZhu2 = "禁用";
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string Getid()
        {
            string id = Request.Form["id"];
            t_GongGaoBiao t_GongGaoBiao = t_GongGaoBiaoService.GetId(Convert.ToInt32(id));
            string shuju = "{\"neiRong\":\"" + t_GongGaoBiao.GongGaoNeiRong + "\"}";
            return shuju;
        }

        public string Update()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var GongGao = _context.t_GongGaoBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                GongGao.GongGaoNeiRong = Request.Form["neirong"].ToString();
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

    }
}
